<?php

namespace App\Http\Controllers\Admin\System;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    private $path;

    public function __construct(Request $request)
    {
        $this->path = isset($request->path) ? $request->path : getcwd();
        view()->share(['path' => $this->path]);
    }

    /***
     * 获取文件列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
//        echo $this->path;exit;
        $files = dir_info($this->path);
//        return $files;
        return view('admin.system.file.index', compact('files'));
//        return view('admin.system.file.index');
    }

    /***
     * 编辑普通文件
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        $contents = file_get_contents($this->path . '/' . $request->name);
        return response()->json(compact('contents'));
    }

    /***
     * 执行编辑普通文件
     * @param Request $request
     */
    public function update(Request $request)
    {
        rename($this->path . '/' . $request->old_name, $this->path . '/' . $request->name);
        file_put_contents($this->path . '/' . $request->name, $request->input('content'));
    }

    /***
     * 上传文件
     * @param Request $request
     */
    function upload(Request $request)
    {
        $dir = explode('public', $this->path);
        Storage::disk('real_public')->putFileAs($dir[1], $request->file('file'), $request->file->getClientOriginalName());
    }

    /***
     * 进入目录
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Request $request)
    {
        $files = dir_info($this->path . '/' . $request->name);
        return view('admin.system.file.show', compact('files'));
    }

}
